package com.gadgets.cm01.user.service.impl;

import com.gadgets.cm01.common.GadgetsCm01Constant;
import com.gadgets.cm01.common.GadgetsCm01ExceptionConstant;
import com.gadgets.cm01.user.dto.UserChangePwdDTO;
import com.gadgets.cm01.user.dto.UserInfoOperationDTO;
import com.gadgets.cm01.user.dto.UserInfoQueryDTO;
import com.gadgets.cm01.user.pojo.User;
import com.gadgets.cm01.user.repository.IUserRepository;
import com.gadgets.cm01.user.service.IUserOperationService;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsException;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.repository.ISequeue;
import com.gadgets.framework.common.util.NumberFormatUtil;
import com.gadgets.framework.common.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author UncleXiu
 * @version v1.0.0
 * @data 2020-09-17
 *
 */
@Service
@Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
public class UserOperationServiceImpl implements IUserOperationService {

    @Autowired
    private IUserRepository userRepository;
    @Autowired
    private ISequeue sequeue;

    /**
     * 新增用户信息
     *
     * @param user 用户信息
     *
     * @throws GadgetsException 异常信息
     */
    @Override
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    public void addUserInfo(User user) throws GadgetsException {
        // 生成用户号
        user.setUserNo(NumberFormatUtil.formatSeqNo(sequeue.getSequeueNoFromAim(GadgetsCm01Constant.USER_NO_SEQ_NAME)));
        userRepository.insertUserInfo(user);
    }

    /**
     * 更新用户密码信息
     *
     * @param userChangePwdDTO 更新信息
     *
     * @throws GadgetsException 异常信息
     */
    @Override
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    public int updateUserPwdInfo(UserChangePwdDTO userChangePwdDTO) throws GadgetsException {
        int i = userRepository.updateUserPwdInfo(userChangePwdDTO);
        if(i!=1){
            throw new GadgetsServiceException(GadgetsCm01ExceptionConstant.USER_LOGIN_INFO_ERRROR,new Object[]{});
        }
        return i;
    }

    /**
     * 更新用户状态信息
     *
     * @param operationDTO 操作信息
     *
     * @throws GadgetsException 异常信息
     */
    @Override
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    public int updateUserStatusInfo(UserInfoOperationDTO operationDTO) throws GadgetsException {
        return userRepository.updateUserStatusInfo(operationDTO);
    }

    /**
     * 分页条件查询用户信息
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     *
     * @throws GadgetsException 异常信息
     */
    @Override
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    public List<User> queryUserInfoByCondition(UserInfoQueryDTO queryDTO, Pagination pagination) throws GadgetsException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<User> userList = userRepository.selectUserInfoByCondition(queryDTO, pagination);
        PaginationUtil.getPaginationResultInfo(userList,pagination);
        return userList;
    }

}
